package com.example.app23_graphics;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.FileNotFoundException;

public class BitmapActivity extends AppCompatActivity {

    private static final String TAG = "BitmapActivity";

    private ImageView bitmap1;
    private ImageView bitmap2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_bitmap);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Log.i(TAG, "onCreate()");

        bitmap1 = findViewById(R.id.bitmap1);
        bitmap2 = findViewById(R.id.bitmap2);

        // 加载资源文件中的图片资源并显示
        bitmap1.setImageResource(R.drawable.ic_launcher_background);

        // 加载存储空间中的图片并显示
        Bitmap bitmap = BitmapFactory.decodeFile("/storage/sdcard/ic_launcher.png");
        bitmap2.setImageBitmap(bitmap);
    }

    public void saveImage(View view) {
        // 加载存储空间中的图片并显示
        Bitmap bitmap = BitmapFactory.decodeFile("/storage/sdcard/ic_launcher.png");
        try {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, 
                    openFileOutput("ic_launcher.png", Context.MODE_PRIVATE));
            Toast.makeText(this, "保存完成", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}